﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.Utilities;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;
using NetDon.eBoss.Portal.HtmlTool;

namespace NetDon.OA.Portal.HtmlTool
{
    public partial class eBayCategoryManagement : NetDonPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Bind();

            }
        }


        void Bind()
        {
            var collection = new HtmltoolcategoryruleCollection();
            collection.GetMulti(null);
            this.gridCustomerInfoList.DataSource = collection;
            this.gridCustomerInfoList.DataBind();
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            Bind();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnDeleteDemand_Click(object sender, EventArgs e)
        {

            var customerId = fieldCustomerId.Value.ToInt32();
            if (customerId.HasValue)
            {
                var category = new HtmltoolcategoryruleEntity(customerId.Value);
                if (category.Rule.Trim() == "默认")
                {
                    Alert("默认分类不可以删除!");
                    return;
                }

                //check if used by product description
                var collection = new ProductgroupCollection();
                collection.GetMulti(ProductgroupFields.Categoryruleid == customerId);
                if (collection.Count > 0)
                {
                    Alert("不可以删除。商品描述正在使用该分类");
                    return;
                }

                var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveOrder");
                try
                {
                  
                    trans.Add(category);
                    //delete all style 
                    var styles = new HtmltoolstyleCollection();
                    styles.GetMulti(HtmltoolstyleFields.Categoryruleid == customerId.Value);
                    foreach (var style in styles)
                    {
                        trans.Add(style);
                        style.Delete();
                    }

                    category.Delete();
                    trans.Commit();

                    Bind();
                    Alert("删除成功!");



                }
                catch (Exception)
                {

                    trans.Rollback();
                    Alert("删除失败！");
                    throw;
                }
            }

        }

        protected void btnDeleteStyleSetting_Click(object sender, EventArgs e)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "btnDeleteStyleSetting");
            try
            {
                var batchId = fieldCustomerId.Value;
                var styles = new HtmltoolstyleCollection();
                styles.GetMulti(HtmltoolstyleFields.Batchid == batchId);
                //check if used by product description
                var defaultSiteStyle = styles.FirstOrDefault(style => style.Sitecode == (int)eBaySites.US);
                if (defaultSiteStyle == null) return;
                var collection = new ProductgroupCollection();
                collection.GetMulti(ProductgroupFields.Styleid == defaultSiteStyle.Id);
                if (collection.Count > 0)
                {
                    Alert("不可以删除。商品描述正在使用该款式");
                    return;
                }

      
                foreach (var style in styles)
                {

                    trans.Add(style); style.Delete();
                }
                trans.Commit();

                Bind();
                Alert("删除成功!");
            }
            catch (Exception)
            {
                trans.Rollback();
                Alert("删除失败！");
                throw;
            }
        }


        protected void gridCustomerInfoList_RowDetailCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var issuingOrderId = gridCustomerInfoList.DataKeys[e.Row.RowIndex]["Id"].ToString();
               

                grdStyleSetting.DataSource = new HtmlToolService().GetStyleSettings(issuingOrderId);
                grdStyleSetting.DataBind();

            }
        }

        protected void gridCustomerInfoList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["Id"] = DataBinder.Eval(e.Row.DataItem, "Id").ToString();
            }
        }

        protected void grdStyleSetting_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);

                var batchId = e.Row.Attributes["BatchId"] = Convert.ToString(DataBinder.Eval(entity, "BatchId"));
              
                var categoryRuleId = Convert.ToString(DataBinder.Eval(entity, "Id"));
                e.Row.Attributes["CategoryRuleId"] = categoryRuleId; 

                var btnEdit = (ImageButton)e.Row.FindControl("btnEdit");
                if (btnEdit != null)
                {
                    btnEdit.OnClientClick = string.Format("modifyStyleSetting('{0}',{1});return false;", batchId, categoryRuleId);
                }

                var btnDelete = (ImageButton)e.Row.FindControl("btnDelete");
                if (btnDelete != null)
                {
                    btnDelete.OnClientClick = string.Format("deleteStyleSetting('{0}');return false;", batchId);
                }

            }
        }


    }
}